package com.example;

import com.example.utils.ConnectionFactoryUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

/**
 * @author wangxiyuan
 * @description
 * @createDate 2021/3/24 11:20
 **/
public class Producer {
    private final static String QUEUE_NAME = "helloworld";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = ConnectionFactoryUtils.getConnectionFactory();
        try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            /*
                第一个参数是exchange名称，空字符串表示无名称交换机
             */
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }

}
